package main

import (
	"crypto/tls"
	"encoding/json"
	"fmt"
	//"github.com/mitchellh/mapstructure"
	"io/ioutil"
	"net/http"
	"net/url"
	"time"
)

const listMap = "list_map"
const quote = "quote"
const list = "list"

//const his = "list"

var API_MAP = make(map[string]string)
var API_KEY = "96841f8b-a02b-4f49-84db-8e3c030245f2"

func init() {
	API_MAP[listMap] = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/map"
	API_MAP[quote] = "https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest"
	API_MAP[list] = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest"
	//API_MAP[his] = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/historical"
}
func Get(s string, param map[string]string, obj interface{}, f func(s string)) bool {
	urls := API_MAP[s]

	// 代理服务器地址
	proxyURL, err := url.Parse("http://127.0.0.1:7897")
	if err != nil {
		return false
	}

	// 配置 HTTP Transport
	transport := &http.Transport{
		Proxy: http.ProxyURL(proxyURL),
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true, // 如果使用的是自签名证书，可能需要设置为 true
		},
	}

	req, err := http.NewRequest("GET", urls, nil)

	if err != nil {
		return false
	}

	req.Header.Set("Accepts", "application/json")
	req.Header.Add("X-CMC_PRO_API_KEY", API_KEY)

	if param != nil {
		q := url.Values{}
		for k, v := range param {
			q.Add(k, v)
		}

		req.URL.RawQuery = q.Encode()
	}

	// 创建 HTTP 客户端
	client := &http.Client{
		Transport: transport,
	}

	resp, err := client.Do(req)

	if err != nil {
		fmt.Println("Error making request:", err)
	}
	defer resp.Body.Close()

	// 读取响应
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response body:", err)
	}

	if f != nil {
		f("")
	}
	var data map[string]interface{}

	err = json.Unmarshal(body, &data)

	if err != nil {
		return false
	}

	i := data["data"]

	marshal, err := json.Marshal(i)

	if err != nil {
		return false
	}

	err = json.Unmarshal(marshal, obj)

	if err != nil || obj == nil {
		return false
	}

	return true
}

func GetMap(s string, param map[string]string) []interface{} {
	urls := API_MAP[s]

	// 代理服务器地址
	proxyURL, err := url.Parse("http://127.0.0.1:7897")
	if err != nil {
		return nil
	}

	// 配置 HTTP Transport
	transport := &http.Transport{
		Proxy: http.ProxyURL(proxyURL),
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true, // 如果使用的是自签名证书，可能需要设置为 true
		},
	}

	req, err := http.NewRequest("GET", urls, nil)

	if err != nil {
		return nil
	}

	req.Header.Set("Accepts", "application/json")
	req.Header.Add("X-CMC_PRO_API_KEY", API_KEY)

	if param != nil {
		q := url.Values{}
		for k, v := range param {
			q.Add(k, v)
		}

		req.URL.RawQuery = q.Encode()
	}

	// 创建 HTTP 客户端
	client := &http.Client{
		Transport: transport,
	}

	resp, err := client.Do(req)

	if err != nil {
		fmt.Println("Error making request:", err)
	}
	defer resp.Body.Close()

	// 读取响应
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response body:", err)
	}

	var data map[string]interface{}

	err = json.Unmarshal(body, &data)

	if err != nil {
		return nil
	}

	i := data["data"]

	dataArr := i.([]interface{})
	return dataArr
}

type RespObj struct {
	D []ListItem `json:"data"`
	S Status     `json:"status"`
}

type Status struct {
	Timestamp    time.Time `json:"timestamp"`
	ErrorCode    int       `json:"error_code"`
	ErrorMessage string    `json:"error_message"`
	Elapsed      int       `json:"elapsed"`
	CreditCount  int       `json:"credit_count"`
}
